using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Finance.DTO.FinanceCategory;
using AnQinFrameWorkAPI.Entity.Finance.Entity;
using AnQinFrameWorkAPI.Entity.Finance.View.FinanceCategory;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Finance;

/// <summary>
///     财务分类业务层
/// </summary>
public class FinanceCategoryService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public FinanceCategoryService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #region 获取财务分类列表

    public async Task<RequestResponseModel> GetFinanceCategoryList(FinanceCategorySearchDTO financeCategorySearchDTO)
    {
        //筛选
        var query = _context.FinanceCategory
                .ProjectTo<FinanceCategoryView>(_mapper.ConfigurationProvider)
                .AsNoTracking()
                .WhereIF(a => a.CategoryName.Contains(financeCategorySearchDTO.CategoryName),
                    !string.IsNullOrEmpty(financeCategorySearchDTO.CategoryName))
                .WhereIF(a => a.CreateTime > financeCategorySearchDTO.StartTime, financeCategorySearchDTO.StartTime != null)
                .WhereIF(a => a.CreateTime < financeCategorySearchDTO.EndTime, financeCategorySearchDTO.EndTime != null)
            ;

        //返回列表
        var financeCategoryList = await query
            .OrderBy(a => a.CreateTime)
            .Page(financeCategorySearchDTO.PageIndex, financeCategorySearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<FinanceCategoryView>(financeCategoryList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增财务分类

    public async Task<RequestResponseModel> AddFinanceCategory(FinanceCategoryAddDTO financeCategoryAddDTO)
    {
        //生成新数据
        var financeCategory = _mapper.Map<FinanceCategoryModel>(financeCategoryAddDTO);

        financeCategory.Id = Guid.NewGuid();
        financeCategory.CreateTime = DateTime.Now;

        //新增数据
        _context.FinanceCategory.Add(financeCategory);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(financeCategory.Id, "新增成功");
    }

    #endregion

    #region 更新财务分类

    public async Task<RequestResponseModel> PutFinanceCategory(FinanceCategoryUpdateDTO financeCategoryUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.FinanceCategory, financeCategoryUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("财务分类Id无效");

        //编辑数据
        var financeCategory = _mapper.Map(financeCategoryUpdateDTO, query);

        //更新表
        _context.FinanceCategory.Update(financeCategory);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(financeCategoryUpdateDTO.Id);
    }

    #endregion

    #region 删除财务分类

    public async Task<RequestResponseModel> DeleteFinanceCategory(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.FinanceCategory, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("财务分类Id无效");

        //删除数据
        _context.FinanceCategory.Remove(query);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion
}